<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> 检测设备 </span>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleForm({})" v-auth="'device:inMeasuringEquipment:buttonEdit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ tid: record.tid })">
          {{ record.enterpriseId }}
        </a>
      </template>
      <!-- 新增 actionColumn 插槽 -->
      <template #actionColumn="{ record }">
        <a-button @click="handleForm({ tid: record.tid })" v-auth="'device:inMeasuringEquipment:buttonEdit'" type="text">
          <span style="font-size:14px; color: #52c41a;">编辑</span>
        </a-button>
        <a-button @click="handleDetail({ tid: record.tid })" type="text">
          <span style="font-size: 14px; color: #4169E1;">详情</span>
        </a-button>
        <Popconfirm
          title="是否确认删除该检测设备？"
          ok-text="确认"
          cancel-text="取消"
          @confirm="handleDelete(record)"
        >
          <a-button  v-auth="'device:inMeasuringEquipment:buttonDelete'"  type="text">
            <span style="font-size:14px;color: red">删除</span>
          </a-button>
        </Popconfirm>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <FormDetail @register="registerFormDetailDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsDeviceManagementInMeasuringEquipmentList">
  import { unref, ref,watch } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { inMeasuringEquipmentDelete, inMeasuringEquipmentListData } from '/@/api/device.management/inMeasuringEquipment';
  import { unitEnterpriseDominantListData } from '/@/api/unitmanage/unitEnterpriseDominant';
  import { useDrawer } from '/@/components/Drawer';
  import { officeTreeData } from '/@/api/sys/office';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import FormDetail from './formDetail.vue';
  import { message } from 'ant-design-vue';
  import { Popconfirm } from 'ant-design-vue'; // 引入 Popconfirm 组件

  const { t } = useI18n('device.management.inMeasuringEquipment');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);
  const route = router.currentRoute.value;

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('检测设备表管理'),
  };
  const EnterpriseParams = ref<Recordable>({
    name: ''
  });

  const searchForm: FormProps = {
    baseColProps: { md: 8, lg: 4 },
    labelWidth: 90,
    schemas: [
      {
        label: t(''),
        field: 'enterpriseId',
        fieldLabel: 'enterpriseName',
        component: 'Select',
        componentProps: () => {
          return {
            placeholder: t('请输入主体名称'),
            api: unitEnterpriseDominantListData,
            params: EnterpriseParams.value,
            fieldNames: { label: 'name', value: 'unitCode' },
            resultField: 'list',
            notFoundContent: '未查询到记录',
            filterTreeNode: false,
            allowClear: true,
            filterOption: false,
            immediate: true,
            onSearch: (value: string) => {
              EnterpriseParams.value.name = value;
              console.log(EnterpriseParams.value);
            },
          };
        },
      },
      {
        label: t(''),
        field: 'equipmentName',
        component: 'Input',
        componentProps: {
          placeholder: t('请输入设备名称'),
          allowClear: true
        },
      },
      {
        label: t(''),
        field: 'equipmentTypeId',
        fieldLabel: 'equipmentType',
        component: 'TreeSelect',
        componentProps: {
          placeholder: t('请输入设备类型'),
          dictType: 'testing_equipment_type',
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'status',
        component: 'Select',
        componentProps: {
          placeholder: t('请选择设备状态'),
          options: [
            { label: '正常', value: '正常' },
            { label: '异常', value: '异常' },
            { label: '24H无预警', value: '24H无预警' },
          ],
          allowClear: true,
        },
      },
      {
        label: t(''),
        field: 'imei',
        component: 'Input',
        componentProps: {
          placeholder: t('请输入设备 IMEI'),
          allowClear: true
        },
      },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('所属企业名称'),
      dataIndex: 'enterpriseName',
      key: 'a.enterprise_name',
      sorter: true,
      width: 130,
      align: 'left',
    },
    {
      title: t('设备名称'),
      dataIndex: 'equipmentName',
      key: 'a.equipment_name',
      sorter: true,
      width: 100,
      align: 'left',
    },
    // {
    //   title: t('设备类型'),
    //   dataIndex: 'equipmentType',
    //   key: 'a.equipment_type',
    //   sorter: true,
    //   width: 130,
    //   align: 'left',
    // },
    {
      title: t('设备 IMEI'),
      dataIndex: 'imei',
      key: 'a.imei',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('设备类型'),
      dataIndex: 'equipmentType',
      key: 'a.equipment_type',
      sorter: true,
      width: 100,
      align: 'left',
    },
    {
      title: t('检测间隔'),
      dataIndex: 'jctime',
      key: 'a.jctime',
      sorter: true,
      width: 60,
      align: 'left',
    },
    {
      title: t('设备状态'),
      dataIndex:'status',
      key: 'a.status',
      sorter: true,
      width: 60,
      align: 'left',
      dictType: 'fire_host_type',
    },
    {
      title: t('卡号'),
      dataIndex: 'cardCode',
      key: 'a.card_code',
      sorter: true,
      width: 90,
      align: 'left',
    },
    {
      title: t('操作'),
      dataIndex: 'action',
      key: 'action',
      width: 130,
      fixed: 'right',
      slot: 'actionColumn' // 指定使用 actionColumn 插槽
    }
  ];

  const [registerTable, { reload }] = useTable({
    api: inMeasuringEquipmentListData,
    beforeFetch: (params) => {
      const urlParams = new URLSearchParams(route.fullPath.split('?')[1]);
      const tidFromUrl = urlParams.get('tid');
      if (tidFromUrl) {
        params.tid = decodeURIComponent(tidFromUrl);
      }
      return params;
    },
    columns: tableColumns,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
  });

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerFormDetailDrawer, { openDrawer: openFormDetailDrawer }] = useDrawer();

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleDetail(record: Recordable) {
    openFormDetailDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const params = { tid: record.tid };
    const res = await inMeasuringEquipmentDelete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  function confirmDelete(record: Recordable) {
    message.confirm({
      title: t('是否确认删除检测设备表'),
      onOk: () => handleDelete(record)
    });
  }

</script>